<?php

Class LeshuaHelper {
    // 测试环境地址
    private $url = "https://t-paygate.lepass.cn/cgi-bin/lepos_pay_gateway.cgi";
    // 正式环境地址
    // private $url = "https://paygate.leshuazf.com/cgi-bin/lepos_pay_gateway.cgi";

    private $merchant_id = '2088010004628975';
    private $key = '2AA09E29FB214E7333483373F65687AE';

    // 交易成功回调地址，如不接收回调不必提供 
    private $callback_url = "2352790EBA1B8DAEF2960162D24471CC";

    public function __construct($merchant_id = '', $key = '') {
        if(!empty($merchant_id)){
            $this->merchant_id = $merchant_id;
        }
        if(!empty($key)){
            $this->key = $key;
        }
    }

    /**
     * 生成指定金额的二维码，服务窗支付
     * 仅支持支付宝，微信已经停用此支付方式
     */
    public function getTdCode($amount, $order_id){
        $param = array('merchant_id'=>$this->merchant_id,
            'service'=>'get_tdcode',
            'pay_way'=>'WXZF',
            'body'=>'支付',
            'jspay_flag'=>0,
            'nonce_str'=>$this->nonce_str(),
            'sub_openid'=>'',
            'third_order_id'=>$order_id,
            'amount'=>$amount,
            'notify_url'=>$this->callback_url,
            't0'=>0,
            'callback_url'=>$this->callback_url,
            'pay_way'=>'ZFBZF');
        $param_str = $this->sign($param);
        echo($param_str.PHP_EOL);

        //response
        $filecontent = $this->post($this->url, $param_str);
        echo($filecontent).PHP_EOL;
        $re_obj = simplexml_load_string($filecontent);
        // var_dump($re_obj);
        return $re_obj;
    }

    public function queryOrder($args){
        $param = array('merchant_id'=>$this->merchant_id,
            'service'=>'query_status',
            'nonce_str'=>$this->nonce_str(),
            'third_order_id'=>$args['order'],
        );

        //request
        $param_str = $this->sign($param);
        echo($param_str.PHP_EOL);

        //response
        $filecontent = $this->post($this->url, $param_str);
        echo($filecontent).PHP_EOL;
        $re_obj = simplexml_load_string($filecontent);
        return $re_obj;
    }

    /**
     * 被扫支付，刷卡支付
     */
    public function upTdCode($amount, $order_id, $tdcode){
        $param = array('merchant_id'=>$this->merchant_id,
            'service'=>'upload_authcode',
            'pay_way'=>'WXZF',
            'body'=>'支付',
            'auth_code'=>$tdcode,
            'nonce_str'=>$this->nonce_str(),
            'third_order_id'=>$order_id,
            'amount'=>$amount,
            'notify_url'=>$this->callback_url,
            't0'=>0,'callback_url'=>$this->callback_url);
        $param_str = $this->sign($param);
        echo($param_str.PHP_EOL);

        //response
        $filecontent = $this->post($this->url, $param_str);
        echo($filecontent).PHP_EOL;
        $re_obj = simplexml_load_string($filecontent);
        // var_dump($re_obj);
        return $re_obj;
    }


    //退款
    public function refund($amount, $order_id){
        $param = array('merchant_id'=>$this->merchant_id,
            'service'=>'unified_refund',
            'nonce_str'=>$this->nonce_str(),
            'third_order_id'=>$order_id,
            'refund_amount'=>$amount,
        );

        $param['merchant_refund_id'] = $param['third_order_id'].'T1';
        $param_str = $this->sign($param);

        //response
        $filecontent = $this->post($this->url, $param_str);
        $re_obj = simplexml_load_string($filecontent);
        return $re_obj;
    }

    public function post($url, $data){
        $ch = curl_init();
        $header = array();
        $header[] = 'Content-Type:application/x-www-form-urlencoded';
        echo $url.PHP_EOL;
        curl_setopt($ch, CURLOPT_URL, $this->url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_HEADER, 0);//设定是否输出页面内容
        //curl_setopt($ch,CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POST, 1); // post,get 过去
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不验证证书
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1 );
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,10);

        //curl_setopt($ch, CURLOPT_PROXY, '192.168.0.12'); //代理服务器地址
        //curl_setopt($ch, CURLOPT_PROXYPORT, 8888); //代理服务器端口
        echo('post ' . $url . '?' . $data);
        $filecontent = curl_exec($ch);
        echo($filecontent);
        return $filecontent;
    }

    /**
     * 参数签名
     */
    private function sign($param){
        ksort($param);
        $mparam = $param;
        $queryParam_arr = array();
        foreach($param as $k=>$v){
            $queryParam_arr[] = $k."=".$v;
        }
        $queryParam = implode("&", $queryParam_arr);
        $sign = strtoupper(md5($queryParam.'&key='.$this->key));
        $mqueryParam_arr = array();
        foreach($mparam as $k=>$v){
            $mqueryParam_arr[$k] = $v;
        }
        $mqueryParam_arr['sign'] = $sign;
        echo($sign);
        return  http_build_query($mqueryParam_arr);
    }

    /**
     *
     */
    private function nonce_str() {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $length = strlen($characters);
        $nonce_str = '';
        for ($i = 0; $i < 16; $i++) {
            $nonce_str .= $characters[rand(0, $length - 1)];
        }
        return $nonce_str;
    }
}

/**
 * 生成商户自己的订单号，乐刷会返回乐刷的订单号，不是这里生成的
 * 接口字段中的third_order_id
 */
function generate_order_id() {
    return strftime('%Y%m%d%H%M%S', time()) . rand(1000000, 9000000);
}

//调用示例
$ls_pay = new LeshuaHelper('0000000018', 'a1613a0e7cb9d3a51e33784ee4d212ac');
// $order_id = generate_order_id();
// $re = $ls_pay->getTdCode(1, $order_id);
// $re = $ls_pay->queryOrder(array('order'=>$order_id));
// 
// $order_id = generate_order_id();
// $re = $ls_pay->upTdCode(1, $order_id, '135186539606343038');
// $re = $ls_pay->queryOrder(array('order'=>$order_id));

$re = $ls_pay->refund(1, '202003250126478571905');

exit();

?>
